<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once ('./application/controllers/authenticate.php');
class Client extends Authenticate {
	
	const RECORD_PAGE_RANGE = 5;
	const RECORD_PER_PAGE = 20;
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('adv_client_model');
		$this->load->library('html/control/fx_control');
		$this->load->library('html/control/fx_filter');
		$this->load->library('html/control/fx_sort');
		$this->adv_client_model->addAttributeOrder('id', 'desc');
		$this->load->library('upload');
		// Load Client Model
		$this->loadCurrentClient();
	}

	public function loadCurrentClient() 
	{
		try {
			// Load Client Model
			$id = $this->input->getParam('id');
			if ($id) {
				$this->adv_client_model->loadByPK($id);
			}
			return $this->adv_client_model;
		} catch (Exception $ex) {
			return null;
		}
	}
	public function index() 
	{	
		try {
			//Unload Current Client
			$this->st_registry->clearRegistry('admin/adv/client/id');
			//Get Data
			$data = $this->_initGridView();
			//Transfer To View
			$this->load->view('adv/client', $data);
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	public function edit() 
	{
		try {
			$data['model'] = $this->adv_client_model;
			$this->load->view('adv/client/edit', $data);	
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	protected function _initGridView()
	{
		$this->adv_client_model->resetFilterList();
		// Get Paginator Params
		$page = $this->input->getParam('page');
		$recordPerPage = $this->input->getParam('recordPerPage');
		if (!$recordPerPage) {
			$recordPerPage = self::RECORD_PER_PAGE;
		}
		// Get Filter Params	
		$filters = $this->input->post('clientFilter');
		// Filter
		$this->fx_filter->filter($filters, $this->adv_client_model);
		// Get Order Params
		$order = $this->input->post('clientOrder');
		// Sort
		$this->fx_sort->sort($order, $this->adv_client_model);
		// Get total found records
		$found = $this->adv_client_model->countTotal();
		// Paginate
		$this->load->library("tree/paginator/engine/st_paginator_collection");
		$this->load->library("tree/paginator/st_paginator");
		$this->st_paginator_collection->setModel($this->adv_client_model);
		$this->st_paginator->setConfig(array(
			'engine' => $this->st_paginator_collection,
			'pageRange' => self::RECORD_PAGE_RANGE,
			'itemCountPerPage' => $recordPerPage,
			'currentPageNumber' => $page  
		));		
		// Paginate Collection
		$collection = $this->st_paginator->getItemsByPage($page);
		if (empty($collection)) {
			$collection = array();
		}
		$data = array();
		$data['client_selected'] = $this->input->post('client_selected');
		$data['found'] = $found;
		$data['client_ids'] = $this->input->post('client_ids');
		$data['category_id'] = $this->input->post('category_id');
		$data['client_ids_array'] = explode(',', $this->input->post('client_ids'));
		
		/*END RADIO SELECTED*/
		$data['model'] = $this->adv_client_model;
		$data['sorter'] = $this->fx_sort;
		$data['filters'] = $this->fx_filter;
		$data['paginator'] = $this->st_paginator;
		$data['collection'] = $collection;
		return $data;
	}
	 
	public function save() 
	{
		try {
			// Load Client Model
			$id = $this->input->post('id');
			if ($id) {
				$this->adv_client_model->loadByPK($id);
			}
			$data = $this->input->post('client');
			if (is_array($data) && count($data) > 0) {
				$this->adv_client_model->setEntireData($data);
				$this->adv_client_model->save();
				$id = $this->adv_client_model->getId();
				$this->st_registry->register('admin/adv/client/message', "Success: Save Successfully");
			}
			if ($id) {
				redirect(site_url("admin/adv/client/edit/id/".$this->adv_client_model->getId(), true));
			} else {
				redirect(site_url("admin/adv/client/index", true));
			}
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/client/message', "Error: when deleting client");
			redirect(site_url("admin/adv/client/index", true));
		}
	}
	
	public function delete()
	{
		try {
			if ($this->adv_client_model->isLoaded()) {
				$name = $this->adv_client_model->getName();
				$this->adv_client_model->delete();
				$this->st_registry->register('admin/adv/client/message', "Delete client $name successfully");
				redirect(site_url("admin/adv/client/index", true));
			}
			else {
				$this->st_registry->register('admin/adv/client/message', "Cannot delete client");
				redirect(site_url("admin/adv/client/index", true));
			}
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/client/message', "Error when deleting client");
			redirect(site_url("admin/adv/client/index", true));
		}
	}
	
	public function massDelete()
	{
		try {
			$ids = $this->input->getMassValues('client_ids');
			if ($ids) {
				$this->adv_client_model->query("delete from adv_client where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/adv/client/message', "Delete [$countItem] clients successfully");
			redirect(site_url("admin/adv/client/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/client/message', $e->getMessage());
			redirect(site_url("admin/adv/client/index", true));
		}
	}
}